package com.generate.main;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.generate.bean.ColumnBean;
import com.generate.bean.HBMGenBean;

public class XMLExplain {

	private String xmlFile;

	public XMLExplain(String xmlFile) {
		this.xmlFile = xmlFile;
	}

	@SuppressWarnings("unchecked")
	public HBMGenBean getHBMBeanFormXML() throws XMLException {
		SAXReader saxReader = new SAXReader();
		Document doc = null;
		File file = new File(xmlFile);
		System.out.println(":File path is::" + file.getPath());
		try {
			doc = saxReader.read(file);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 根结点为：hibernate-mapping
		Element root = doc.getRootElement();
		// 取根下class结点
		Iterator<Element> clsElemts = root.elementIterator("class");
		if (!clsElemts.hasNext()) {
			throw new XMLException("HBM文件有问题！");
		}
		Element clsElemt = clsElemts.next();

		// 取映射的类名
		String className = clsElemt.attributeValue("name");
		if (StringUtils.isBlank(className)) {
			throw new XMLException("HBM文件配置有误！");
		}
		HBMGenBean rtnBean = new HBMGenBean();
		// 拆分类名
		int pckgLo = className.lastIndexOf(".");// 取最后一个点
		String packageName = className.substring(0, pckgLo);
		className = className.substring(pckgLo + 1);
		// 提取程序标识（取包名最后一个）
		pckgLo = packageName.lastIndexOf(".");
		String codeMark = packageName.substring(pckgLo + 1);
		// 设置Java基本值
		rtnBean.setPckgName(packageName);
		rtnBean.setCodeMark(codeMark);
		rtnBean.setPoName(className);
		// 数据表相关内容
		rtnBean.setTablename(clsElemt.attributeValue("table"));
		rtnBean.setSchema(clsElemt.attributeValue("schema"));
		/**
		 * 分解字段
		 */
		List<ColumnBean> cols = new ArrayList<ColumnBean>();
		// 提取主键（只能为一个）
		boolean hasIdKey = true;// HBM文件是否包含主键
		int idColumnts = 0;// 字段数
		String pkName = null;
		Iterator<Element> idElemts = clsElemt.elementIterator("id");
		if (idElemts.hasNext()) {
			Element idElemt = idElemts.next();
			String colName = idElemt.attributeValue("name");// ID 列名： <id>下Name
			String colType = idElemt.attributeValue("type");// ID 列类型： <id>下type
			
			ColumnBean idCol = getColumBean(colName,colType,true);
			pkName = colName;
			idColumnts++;
			cols.add(idCol);
			
		} else {
			hasIdKey = false;
		}
		idElemts = null;
		rtnBean.setPmKey(pkName);
		// 取复合主键
		idElemts = clsElemt.elementIterator("composite-id");
		if (idElemts.hasNext()) {
			// @@@ 未完
		}
		idElemts = null;
		// 取property 字段
		Iterator<Element> colElemts = clsElemt.elementIterator("property");
		while (colElemts.hasNext()) {
			Element colElemt = colElemts.next();
			String colName = colElemt.attributeValue("name");// ID 列名： <id>下Name
			String colType = colElemt.attributeValue("type");// ID 列类型：
																// <id>下type

			ColumnBean idCol = getColumBean(colName,colType,false);
			cols.add(idCol);
		}
		/**
		 * 取一多对列
		 * @@ 未完
		 */
		//设置字段
		rtnBean.setColSize(cols.size());
		rtnBean.setColumnBeans(cols);
		return rtnBean;
	}

	protected ColumnBean getColumBean(String colName,String colType,boolean isId){
		ColumnBean rtn = new ColumnBean();
		if("timestamp".equalsIgnoreCase(colType)){
			rtn.setDateTime(true);
		}
		rtn.setId(isId);
		rtn.setColumnName(colName);
		rtn.setDataTypeName(colType);
		return rtn;		
	}
	
	
	/**
	 * @return the xmlFile
	 */
	public String getXmlFile() {
		return xmlFile;
	}

	/**
	 * @param xmlFile
	 *            the xmlFile to set
	 */
	public void setXmlFile(String xmlFile) {
		this.xmlFile = xmlFile;
	}
}
